﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Lab1_Mochulskiy_.Occurances;

namespace Lab1_Mochulskiy_
{
    public class Recognition
    {
        private readonly List<OccurancePatternBase> _recognitionPatterns = new List<OccurancePatternBase>();

        public Recognition()
        {
            _recognitionPatterns.Add(new BikePattern());
            _recognitionPatterns.Add(new MotorcyclePattern());
            _recognitionPatterns.Add(new CarPattern());
            _recognitionPatterns.Add(new BusPattern());
            _recognitionPatterns.Add(new TruckPattern());
        }

        public string Recognize(Criteria criteria)
        {
            string returnResult = "Unrecognized";
            var returnRes = new List<string>();
            int max = 0;

            foreach (var pattern in _recognitionPatterns)
            {
                if (pattern.GetOccuranceCount(criteria) > max)
                {
                    max = pattern.GetOccuranceCount(criteria);
                    returnRes.Add(pattern.Name);
                }
                else if (pattern.GetOccuranceCount(criteria) == max && max > 0)
                {
                    returnRes.Add(pattern.Name);
                }
            }

            if (returnRes.Count > 0)
                return string.Join(" or " , returnRes.ToArray());

            return returnResult;
        }

        public string GetPatternsInfo()
        {
            StringBuilder info = new StringBuilder();

            foreach (var pattern in _recognitionPatterns)
            {
                info.AppendFormat(string.Format("{0}:\n  Weight = {1}..{2}\n  Wheels Count = {3}\n  Maximum speed = {4}..{5}\n",
                    pattern.Name, pattern.MinWeight, pattern.MaxWeight, pattern.WheelsCount, pattern.MaxSpeed - 10, pattern.MaxSpeed + 10));
            }

            return info.ToString();
        }
    }
}
